/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.goal;

import java.util.ArrayList;
import java.util.List;

import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.model.equipment.EquipmentModel;
import ca.usherbrooke.model.world.IWorldModel;
import ca.usherbrooke.tools.position.Vector2D;

/**
 * @author Cody Stoutenburg
 * 
 */
public final class SearchEquipmentGoal extends IGoal {
	private static String GOAL_NAME = SearchEquipmentGoal.class.getName();

	public static String getGeneralDescription() {
		return "<html>"
				+ "<p><b>Description:</b></p>"
				+ "<p>Ask to search an equipment. </p> "
				+ "<p style=\"color:green\">The goal is solve when the entity find the equipment.</p>"
				+ "<p style=\"color:red\"> The goal is fail when the time is out </p>"
				+ "<br/>" + "<b>Params:</b><br/> " + " equipmentId(long) </p>"
				+ "</html>";
	}

	private Long guidEquipment;

	@Override
	public void initGoal(String args) {
		String[] arguments = args.split(ARGS_SEPARATOR);

		guidEquipment = Long.valueOf(arguments[argsCount + 0]);
		argsCount += 1;
	}

	@Override
	public void askAddSubGoal(BasicGoal goal) {
		// do nothing we do not want sub goal
	}

	@Override
	public Boolean verifyGoalSolve(IWorldModel world) {
		Boolean isSolve = false;
		IEntityModel entity = world.getEntity(getEntity());
		List<EquipmentModel> allEq = world
				.getEquipment(entity.getViewingArea());

		for (EquipmentModel equipmentModel : allEq) {
			if (equipmentModel.getGuid().equals(guidEquipment)) {
				isSolve = true;
			}
		}
		return isSolve;
	}

	@Override
	public Boolean verifyGoalFail(IWorldModel world) {
		// this goal never fail unless when time is out
		return false;
	}

	@Override
	public Float calculateEstimatedDistance(IWorldModel world) {
		Float distance = Float.MAX_VALUE;

		EquipmentModel eq = world.getEquipment(guidEquipment);
		IEntityModel entity = world.getEntity(getEntity());

		if (eq != null && entity != null) {
			Vector2D depart = entity.getPosition();
			Vector2D destination = eq.getPosition();

			distance = depart.getDistance(destination);
		}

		return distance;
	}

	@Override
	public ArrayList<Float> getValues() {
		ArrayList<Float> allVals = new ArrayList<Float>();
		allVals.add(guidEquipment.floatValue());
		return allVals;
	}

	@Override
	public String getName() {
		return GOAL_NAME;
	}

	@Override
	public String getDescription() {
		return "search equipment " + this.guidEquipment;
	}
}
